<?php
namespace App\Api\Controllers\Pay;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use App\Models\Order;
use App\Common\PaySuccessAction;
/**
 * 扫码设备支付接口
 * Class IndexController
 * @package App\Api\Controllers\DevicePay
 */
class IndexController extends BaseController
{

    //扫一扫收款
    public function print()
    {
        $ds=array();
        $stores=DB::table('pingan_stores')->select('store_id','merchant_id')->get();
        foreach($stores as $k=>$v){
            $wincode_stores=DB::table('wincode_stores')->where('store_id',$v->store_id)->first();
            if(!empty($wincode_stores)){
                $ds[$k]=$v->merchant_id;
            }
            $qwx_store=DB::table('qwx_store')->where('store_id',$v->store_id)->first();
            if(!empty($qwx_store)){
                $ds[$k]=$v->merchant_id;
            }
            $glow_stores=DB::table('glow_stores')->where('store_id',$v->store_id)->first();
            if(!empty($glow_stores)){
                $ds[$k]=$v->merchant_id;
            }
        }
        $ds=array_unique($ds);
            echo '<pre>';
            print_r($ds);
            die;
    }
    
     //扫一扫收款
    public function pingan(Request $request)
    {
        $data = $request->all();
        if (isset($data['third_order_id'])) {
                $trade_no = $data['third_order_id'];
                $day = date('Ymd', time());
                $table = 'orders_' . $day;
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('trade_no', $trade_no)->first();
                } else {
                    $order = Order::where('trade_no', $trade_no)->first();
                }
                $pingan_stores=DB::table('pingan_stores')->where('merchant_id', $data['merchant_id'])->first();
                $store=DB::table('stores')->where('store_id', $pingan_stores->store_id)->first();
                $total_money=$data['amount'];
                //系统订单未成功
                if(empty($order)){
                    $created_at=date('Y-m-d H:i:s', time());
                    $out_trade_no = date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
                    $order_data=array(
                        'config_id'=>'1234',
                        'out_trade_no'=>$out_trade_no,
                        'trade_no'=>$trade_no,
                        'store_id'=>$store->store_id,
                        'merchant_name'=>'店长',
                        'user_id'=>$store->user_id,
                        'merchant_id'=>$store->merchant_id,
                        'store_name'=>$store->store_name,
                        'pay_method'=>'qr_code',
                        'company'=>'pingan',
                        'total_amount'=>$total_money,
                        'receipt_amount'=>$total_money,
                        'buyer_pay_amount'=>$total_money,
                        'pay_amount'=>$total_money,
                        'fee_amount'=>'0.00',
                        'shop_price'=>$total_money,
                        'rate'=>'0.00',
                        'shop_count'=>1,
                        'status'=>1,
                        'pay_status'=>1,
                        'pay_status_desc'=>'支付成功',
                        'pay_time'=>$created_at,
                        'created_at'=>$created_at,
                        'updated_at'=>$created_at
                    );
                    if($data['pay_way']=='WXZF'){
                        $order_data['ways_type']='30011';
                        $order_data['ways_type_desc']='微信支付';
                        $order_data['ways_source']='weixin';
                        $order_data['ways_source_desc']='微信支付';
                    }else if($data['pay_way']=='ZFBZF'){
                        $order_data['ways_type']='30012';
                        $order_data['ways_type_desc']='支付宝支付';
                        $order_data['ways_source']='alipay';
                        $order_data['ways_source_desc']='支付宝支付';
                    }else if($data['pay_way']=='UPSMZF'){
                        $order_data['ways_type']='30013';
                        $order_data['ways_type_desc']='云闪付支付';
                        $order_data['ways_source']='unionpay';
                        $order_data['ways_source_desc']='云闪付支付';
                    }
                    DB::table('orders')->insert($order_data);
                    $order = Order::where('trade_no', $trade_no)->first();
                    //支付成功后的动作
                    $data1 = [
                        'ways_type' => $order->ways_type,
                        'company' => $order->company,
                        'ways_type_desc' => $order->ways_type_desc,
                        'source_type' => '30011',//返佣来源
                        'source_desc' => '平安银行',//返佣来源说明
                        'total_amount' => $order->total_amount,
                        'out_trade_no' => $order->out_trade_no,
                        'other_no' => $order->other_no,
                        'rate' => $order->rate,
                        'fee_amount' => $order->fee_amount,
                        'merchant_id' => $order->merchant_id,
                        'store_id' => $order->store_id,
                        'user_id' => $order->user_id,
                        'config_id' => $order->config_id,
                        'store_name' => $order->store_name,
                        'ways_source' => $order->ways_source,
                        'pay_time' => $order->pay_time,
                        'device_id' => $order->device_id,
                    ];
                    PaySuccessAction::action($data1);
                    return json_encode([
                        'code' => 'success',
                        'msg' => "成功",
                    ]);
            }
        }
        return json_encode([
            'code' => 'success',
            'msg' => "成功",
        ]);
    }
    
    //扫一扫收款
    public function pingan1(Request $request)
    {
        $data = $request->all();
        $data=array (
  'merchant_id' => '307989956210166',
  'amount' => '18409.00',
  'third_order_id' => '20220629223921600182',
  'pay_way' => 'UPSMZF',
);
        if (isset($data['third_order_id'])) {
                $trade_no = $data['third_order_id'];
                $day = date('Ymd', time());
                $table = 'orders_' . $day;
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('trade_no', $trade_no)->first();
                } else {
                    $order = Order::where('trade_no', $trade_no)->first();
                }
                $pingan_stores=DB::table('pingan_stores')->where('merchant_id', $data['merchant_id'])->first();
                $store=DB::table('stores')->where('store_id', $pingan_stores->store_id)->first();
                $total_money=$data['amount'];
                //系统订单未成功
                if(empty($order)){
                    $created_at=date('Y-m-d h:i:s', time());
                    $out_trade_no = 'pingan' . date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
                    $order_data=array(
                        'config_id'=>'1234',
                        'out_trade_no'=>$out_trade_no,
                        'trade_no'=>$trade_no,
                        'store_id'=>$store->store_id,
                        'merchant_name'=>'店长',
                        'user_id'=>$store->user_id,
                        'merchant_id'=>$store->merchant_id,
                        'store_name'=>$store->store_name,
                        'pay_method'=>'qr_code',
                        'company'=>'pingan',
                        'total_amount'=>$total_money,
                        'receipt_amount'=>$total_money,
                        'buyer_pay_amount'=>$total_money,
                        'pay_amount'=>$total_money,
                        'fee_amount'=>'0.00',
                        'shop_price'=>$total_money,
                        'rate'=>'0.00',
                        'shop_count'=>1,
                        'status'=>1,
                        'pay_status'=>1,
                        'pay_status_desc'=>'支付成功',
                        'pay_time'=>$created_at,
                        'created_at'=>$created_at,
                        'updated_at'=>$created_at
                    );
                    if($data['pay_way']=='WXZF'){
                        $order_data['ways_type']='30011';
                        $order_data['ways_type_desc']='微信支付';
                        $order_data['ways_source']='weixin';
                        $order_data['ways_source_desc']='微信支付';
                    }else if($data['pay_way']=='ZFBZF'){
                        $order_data['ways_type']='30012';
                        $order_data['ways_type_desc']='支付宝支付';
                        $order_data['ways_source']='alipay';
                        $order_data['ways_source_desc']='支付宝支付';
                    }else if($data['pay_way']=='UPSMZF'){
                        $order_data['ways_type']='30013';
                        $order_data['ways_type_desc']='云闪付支付';
                        $order_data['ways_source']='unionpay';
                        $order_data['ways_source_desc']='云闪付支付';
                    }
                    DB::table('orders')->insert($order_data);
                    $order = Order::where('trade_no', $trade_no)->first();
                    //支付成功后的动作
                    $data1 = [
                        'ways_type' => $order->ways_type,
                        'company' => $order->company,
                        'ways_type_desc' => $order->ways_type_desc,
                        'source_type' => '30011',//返佣来源
                        'source_desc' => '平安银行',//返佣来源说明
                        'total_amount' => $order->total_amount,
                        'out_trade_no' => $order->out_trade_no,
                        'other_no' => $order->other_no,
                        'rate' => $order->rate,
                        'fee_amount' => $order->fee_amount,
                        'merchant_id' => $order->merchant_id,
                        'store_id' => $order->store_id,
                        'user_id' => $order->user_id,
                        'config_id' => $order->config_id,
                        'store_name' => $order->store_name,
                        'ways_source' => $order->ways_source,
                        'pay_time' => $order->pay_time,
                        'device_id' => $order->device_id,
                    ];
                    PaySuccessAction::action($data1);
                    return json_encode([
                        'code' => 'success',
                        'msg' => "成功",
                    ]);
            }
        }
        return json_encode([
            'code' => 'success',
            'msg' => "成功",
        ]);
    }
}
